package com.example.city_home_app.Activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.AppCompatRadioButton;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.CardView;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.AccelerateInterpolator;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.example.city_home_app.Bean.Down_Captcha;
import com.example.city_home_app.Bean.Down_City;
import com.example.city_home_app.Bean.Down_Universe;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RetrofitListener;
import com.example.city_home_app.UI.UIUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {


    @BindView(R.id.et_regloginname)
    EditText etRegloginname;
    @BindView(R.id.et_regpassword)
    EditText etRegpassword;
    @BindView(R.id.et_repeatpassword)
    EditText etRepeatpassword;
    @BindView(R.id.et_regusername)
    EditText etRegusername;
    @BindView(R.id.et_regemail)
    EditText etRegemail;
    @BindView(R.id.btn_reg_getcheckcode)
    Button btnRegGetcheckcode;
    @BindView(R.id.et_regcheckcode)
    EditText etRegcheckcode;
    @BindView(R.id.rdo_man)
    AppCompatRadioButton rdoMan;
    @BindView(R.id.rdo_woman)
    AppCompatRadioButton rdoWoman;
    @BindView(R.id.bt_go)
    Button btGo;
    @BindView(R.id.cv_add)
    CardView cvAdd;
    @BindView(R.id.fab)
    FloatingActionButton fab;
    @BindView(R.id.spinner_reg_city)
    AppCompatSpinner spinnerRegCity;
    @BindView(R.id.radiogroup_reg_gender)
    RadioGroup radiogroupRegGender;

    private int codeid, cityid;
    private int gender = 1;
    private String code;
    private String email;

    private List<Down_City> city_list;

    private RetrofitListener<Down_Captcha> captchaRetrofitListener = new RetrofitListener<Down_Captcha>() {
        @Override
        public void onResponse(Down_Captcha listener) {
            codeid = listener.getId();
            code = listener.getCode();
            UIUtils.showToast(RegActivity.this, "已发送邮件，请前往邮箱查收验证码", UIUtils.LENGTH_SHORT);
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private RetrofitListener<Down_Universe> regretrofitListener = new RetrofitListener<Down_Universe>() {
        @Override
        public void onResponse(Down_Universe listener) {
            if(listener.getMessage().equals("success")){
                UIUtils.showToast(RegActivity.this, "注册成功", UIUtils.LENGTH_SHORT);
                animateRevealClose();
            }else{
                UIUtils.showToast(RegActivity.this, listener.getMessage(), UIUtils.LENGTH_SHORT);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private RetrofitListener<List<Down_City>> cityretrofitListener = new RetrofitListener<List<Down_City>>() {
        @Override
        public void onResponse(List<Down_City> listener) {
            city_list = listener;
            List<String> list = new ArrayList<>();
            for (Down_City bean : listener) {
                list.add(bean.getCity_name());
            }
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(RegActivity.this, android.R.layout.simple_spinner_item, list);
            arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            spinnerRegCity.setAdapter(arrayAdapter);
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_reg;
    }

    @Override
    protected void initViews() {
        super.initViews();
        ButterKnife.bind(this);
        ShowEnterAnimation();

        radiogroupRegGender.setOnCheckedChangeListener(this);
        setspinnerselect();

    }

    @Override
    protected void initData() {
        super.initData();
        ApiModel model = new ApiModel();
        model.getcitylist(cityretrofitListener);
    }


    private void ShowEnterAnimation() {
        Transition transition = TransitionInflater.from(this).inflateTransition(R.transition.fabtransition);
        getWindow().setSharedElementEnterTransition(transition);

        transition.addListener(new Transition.TransitionListener() {
            @Override
            public void onTransitionStart(Transition transition) {
                cvAdd.setVisibility(View.GONE);
            }

            @Override
            public void onTransitionEnd(Transition transition) {
                transition.removeListener(this);
                animateRevealShow();
            }

            @Override
            public void onTransitionCancel(Transition transition) {

            }

            @Override
            public void onTransitionPause(Transition transition) {

            }

            @Override
            public void onTransitionResume(Transition transition) {

            }


        });
    }

    public void animateRevealShow() {
        Animator mAnimator = ViewAnimationUtils.createCircularReveal(cvAdd, cvAdd.getWidth() / 2, 0, fab.getWidth() / 2, cvAdd.getHeight());
        mAnimator.setDuration(500);
        mAnimator.setInterpolator(new AccelerateInterpolator());
        mAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }

            @Override
            public void onAnimationStart(Animator animation) {
                cvAdd.setVisibility(View.VISIBLE);
                super.onAnimationStart(animation);
            }
        });
        mAnimator.start();
    }

    public void animateRevealClose() {
        Animator mAnimator = ViewAnimationUtils.createCircularReveal(cvAdd, cvAdd.getWidth() / 2, 0, cvAdd.getHeight(), fab.getWidth() / 2);
        mAnimator.setDuration(500);
        mAnimator.setInterpolator(new AccelerateInterpolator());
        mAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                cvAdd.setVisibility(View.INVISIBLE);
                super.onAnimationEnd(animation);
                fab.setImageResource(R.drawable.plus);
                RegActivity.super.onBackPressed();
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
            }
        });
        mAnimator.start();
    }

    @Override
    public void onBackPressed() {
        animateRevealClose();
    }


    @OnClick({R.id.btn_reg_getcheckcode, R.id.bt_go, R.id.fab})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_reg_getcheckcode:
                if (UIUtils.isemail(etRegemail.getText().toString())) {
                    email = etRegemail.getText().toString();
                    ApiModel model = new ApiModel(1);
                    model.getcheckcode(email, captchaRetrofitListener);
                } else {
                    UIUtils.showToast(this, "邮箱格式错误，请重新输入", UIUtils.LENGTH_SHORT);
                }
                break;
            case R.id.bt_go:
                if (!etRegloginname.getText().toString().isEmpty() ||
                        !etRegpassword.getText().toString().isEmpty() ||
                        !etRepeatpassword.getText().toString().isEmpty() ||
                        !etRegemail.getText().toString().isEmpty() ||
                        !etRegcheckcode.getText().toString().isEmpty()) {
                    if (!etRegpassword.getText().toString().equals(etRepeatpassword.getText().toString())) {
                        UIUtils.showToast(this, "两次密码不相同", UIUtils.LENGTH_SHORT);
                        etRegpassword.setText("");
                        etRepeatpassword.setText("");
                    } else if (!UIUtils.isusername(etRegloginname.getText().toString())) {
                        UIUtils.showToast(this, "登录用户名长度限制为6-16位,且不允许特殊字符", UIUtils.LENGTH_SHORT);
                    } else if (!etRegemail.getText().toString().equals(email)) {
                        UIUtils.showToast(this, "邮箱与上一次输入不符", UIUtils.LENGTH_SHORT);
                    } else if (!UIUtils.isname(etRegusername.getText().toString())) {
                        UIUtils.showToast(this, "登录用户名长度限制为2-16位,且不允许特殊字符", UIUtils.LENGTH_SHORT);
                    } else {
                        ApiModel model = new ApiModel();
                        model.reg(etRegloginname.getText().toString(), email, etRegcheckcode.getText().toString(),
                                codeid, cityid, etRegusername.getText().toString(), etRegpassword.getText().toString(),gender, regretrofitListener);
                    }
                } else {
                    UIUtils.showToast(this, "请输入完整信息", UIUtils.LENGTH_SHORT);
                }
                break;
            case R.id.fab:
                animateRevealClose();
                break;
        }


    }

    private void setspinnerselect(){
        spinnerRegCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                cityid = city_list.get(i).getCity_id();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i){
            case R.id.rdo_man:
                gender = 1;
                break;
            case R.id.rdo_woman:
                gender = 2;
                break;
        }
    }
}
